// pages/jieguaka/jieguaka.js

const urlAdd = require('../../utils/config').urlAdd;
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 解挂卡
   */
  jiegua_tijiao: function (e) {
    var lost_idcard = e.detail.value.userAccIdcard; //身份证
    var lost_password = e.detail.value.userCardPass; //一卡通密码
    var stuCode = wx.getStorageSync('stuCode'); //学工号
    var lostcardList = this.data.crdAccid; //卡号


    if (lost_idcard == "") {
      wx.showModal({
        title: '提示',
        content: '身份证号不能为空',
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    //验证身份证的正确性
    if ((!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(lost_idcard)))) {
      wx.showModal({
        title: '提示',
        content: '请输入18位的有效身份证',
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    if (lost_password == "") {
      wx.showModal({
        title: "错误",
        content: "一卡通密码不能为空",
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    // 提交
    wx.request({
      url: urlAdd + "/m/tcommcardController/unlockCard?stuCode=" + stuCode + "&idCard=" + lost_idcard + "&cardNo=" + lostcardList + "&pwd=" + lost_password, //接口地址
      data: {
        'stuCode': stuCode,
        'idCard': lost_idcard,
        'cardNo': lostcardList,
        'pwd': lost_password,
      },
      method: "POST",
      header: {
        "content-type": "application/json"
      },
      success: function (result) {

        if (result.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: '解挂成功',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '解挂失败',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
        }

      },
      fail: function (xhr, type, errorThrown) {
        wx.showToast({
          title: '网络请求超时',
          image: '/images/erron.png'
        })
      }
      // 提交
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "解挂",
    });
    var that = this;
    
  },

  findCardByStuCode: function(){
    wx.showLoading({
      title: '查询卡片中...',
    })
    var that = this;
    var stuCode = wx.getStorageSync('stuCode');
    console.log(that);
    wx.request({
      url: urlAdd + '/f/wechat/card/hangCard?weixinOpenId=' + wx.getStorageSync('openId'),
      data: {},
      dataType: 'json', //服务器返回json格式数据
      type: 'get', //HTTP请求类型
      timeout: 10000, //超时时间设置为10秒；
      success: function (result) {
        console.log("result=====")
        console.log(result);
        if (result.data.code == '205') {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '暂无卡片',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
        if (result.data.code == '200') {
          // for (var i = 0, len = result.data.data.length; i < len; i++) {
          //   console.log(result.data.data[i].crdStatus);
          //   if (result.data.data[i].crdStatus == '挂失') {
          //     wx.hideLoading();
          //     that.setData({
          //       stuCode: wx.getStorageSync('stuCode'),
          //       crdAccid: result.data.data[i].crdId,
          //     });
          //     console.log(stuCode)
          //     console.log(crdAccid)
          //     that.jiegua_tijiao();
          //   }
          // }
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '暂无可解挂卡片',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })

        }

      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据超时',
          image: '/images/erron.png',
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('stuCode') == '' || wx.getStorageSync('stuCode') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/login/login'
        })
      } else {
        
        that.findCardByStuCode();
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})